<?php

namespace Api\Service;

use AllowDynamicProperties;
use Carbon\Carbon;
use Clockwork\Request\Log;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use Modules\Film\Models\FilmOrder;
use stdClass;

#[AllowDynamicProperties] class FilmService
{
    public function __construct()
    {
        $this->domain = 'https://api.tickets.yipinyouxiang.com/jeecgboot/';
        $this->appid = env('YIXUANAPPID');
        $this->secret = env('YIXUANSECRET');

        $pem = chunk_split($this->secret, 64, "\n");
        $this->publicKey = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
    }

    /**
     * @param $data
     * @return string
     * 计算签名
     */
    private function getsign($data, $time, $nonce)
    {
        $str = md5($data . "#" . $nonce . "#" . $time);

        openssl_public_encrypt($str, $encryptedData, $this->publicKey);
        return base64_encode($encryptedData);
    }

    /**
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取影院列表
     */
    public function getCinemas($data)
    {
        $url = $this->domain . '/biz/api/tickets/getCinemas';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        try {
            $response = $client->post($url, [
                'headers' => $headers,
                'body' => $json
            ]);
            $result = $response->getBody()->getContents();
            return json_decode($result, true);
        }catch (\Exception $e){
            return false;
        }


    }

    /**
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取全国城市列表
     */
    public function getCityList()
    {
        $url = $this->domain . '/biz/api/tickets/getCityList';
        $json = json_encode(new stdClass());
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    /**
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取影院排期信息
     */
    public function getCinemaSchedule($data)
    {
        $url = $this->domain . '/biz/api/tickets/getCinemaSchedule';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    /**
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 电影院详情
     */
    public function cinemaDetail($data)
    {
        $url = $this->domain . '/biz/api/tickets/cinemaDetail';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    public function filmRegions($data)
    {
        $url = $this->domain . '/biz/api/tickets/filmRegions';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }


    /**
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 选择座位,传入原票价信息,计算出售价数据
     */
    public function checkTicketPrices($data)
    {
        $url = $this->domain . '/biz/api/tickets/checkTicketPrices';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }


    public function getHotMovies(array|string|null $data)
    {
        $url = $this->domain . '/biz/api/tickets/getHotMovies';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client(['verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    /**
     * @param array $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 获取即将上映列表
     */
    public function getInComingMovies($data)
    {
        $url = $this->domain . '/biz/api/tickets/getInComingMovies';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client(['verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    public function getMovieScheduleDetail(array|string|null $data)
    {
        $url = $this->domain . '/biz/api/tickets/getMovieScheduleDetail';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    public function getMovieSeats(array|string|null $data)
    {
        $url = $this->domain . '/biz/api/tickets/getMovieSeats';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client(['verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    public function moviePlayDate(array|string|null $data)
    {
        $url = $this->domain . '/biz/api/tickets/moviePlayDate';
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    /**
     * @param array|string|null $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     * 选座预下单
     */
    public function preOrder(array|string|null $data)
    {
        $url = $this->domain . '/biz/api/tickets/third/preOrder';
        $data["testModel"] = 0;
        $json = json_encode($data);
        $time = Carbon::now()->getTimestampMs();
        $nonce = rand(10000, 99999);
        $accessToken = $this->getSign($json, $time, $nonce);
        $client = new Client([ 'verify' => false]);
        $headers = ['Content-Type' => "application/json",
            "ACCESS-TOKEN" => $accessToken,
            'cps' => $this->appid,
            "nonce" => $nonce,
            "ts" => $time,
        ];
        $response = $client->post($url, [
            'headers' => $headers,
            'body' => $json
        ]);
        $result = $response->getBody()->getContents();
        return json_decode($result, true);
    }

    public function createOrder($orderSn)
    {
        $url = $this->domain . '/biz/api/tickets/third/notify/paynext/'.$orderSn;
        $orderInfo = FilmOrder::where('order_sn', $orderSn)->where('status',0)->select(['ci','cinema_id','show_id','buy_seats','accept_changeseat','mode',
            'phone_no',])->first();
        if(empty($orderInfo)){
            return false;
        }
        $orderInfo = $orderInfo->toArray();
            $param = [];
            foreach ($orderInfo as $k=>$v){
                $str = Str::camel($k);
                if($k == "buy_seats"){
                    $param[$str] = json_decode($v);
                }else{
                    $param[$str] = $v;
                }

            }
            $param['callback'] = 'https://api.susujilianmeng.com/kuaidi/film/callback';
            $param['outId'] = $orderSn;
            $param['testModel'] = 0;
            $post['orderId'] = $orderSn;
            $post['status'] = 'success';
            $post['param'] = $param;
            //api下单
            $json = json_encode($post);
            $time = Carbon::now()->getTimestampMs();
            $nonce = rand(10000, 99999);
            $accessToken = $this->getSign($json, $time, $nonce);
            $client = new Client(['timeout' => 5, 'verify' => false]);
            $headers = ['Content-Type' => "application/json",
                "ACCESS-TOKEN" => $accessToken,
                'cps' => $this->appid,
                "nonce" => $nonce,
                "ts" => $time,
            ];
            $response = $client->post($url, [
                'headers' => $headers,
                'body' => $json
            ]);
            $result = $response->getBody()->getContents();
            return $result;

    }


}
